class PostsController < ResourceController
  def show
    redirect_to topic_post_path(@post)
  end
  
  def new
    if params[:quote_post_id]
      quoted_post = Post.find(params[:quote_post_id])
      @post.content = BBCode.quote(quoted_post.user.name, quoted_post.content)
    end
  end
  
  def create
    @post.user = current_user
    if !@post.valid? || params[:preview_button]
      render :action => 'new'
    else
      @post.save!
      flash[:notice] = "Successfully created post"
      redirect_to topic_post_path(@post)
    end
  end
  
  # render edit
  
  def update
    if !@post.valid? || params[:preview_button] # REFACTORME minor duplication with create action
      render :action => 'edit'
    else
      @post.save!
      flash[:notice] = "Successfully updated post"
      redirect_to topic_post_path(@post)
    end
  end
  
  def destroy
    if @post.eql?(@post.topic.first_post)
      flash[:error] = "Cannot remove the first post in a topic."
    else
      @post.destroy
      flash[:notice] = "Successfully destroyed post"
    end
    redirect_to topic_path(@post.topic_id)
  end
  
  protected
  
    def update_extra_attributes
      @post.topic_id = params[:topic_id] unless params[:topic_id].nil?
    end
end
